package com.biboheart.adapter.express.support;

import com.biboheart.adapter.express.actuator.GenericActuator;
import com.biboheart.adapter.support.utils.ObjectTypeUtils;

import java.math.BigDecimal;

final class MultiplyActuator implements GenericActuator {
    @Override
    public Object execute(Object source, Object... args) {
        if (null == source || args.length == 0) {
            return null;
        }
        Object multiplier = args[0];
        if (!(source instanceof Number) || !(multiplier instanceof Number)) {
            return null;
        }
        if ((source instanceof Double) || (multiplier instanceof Double)) {
            return BigDecimal.valueOf(ObjectTypeUtils.convertType(source, Double.class)).multiply(BigDecimal.valueOf(ObjectTypeUtils.convertType(multiplier, Double.class))).doubleValue();
        }
        if ((source instanceof Float) || (multiplier instanceof Float)) {
            return BigDecimal.valueOf(ObjectTypeUtils.convertType(source, Float.class)).multiply(BigDecimal.valueOf(ObjectTypeUtils.convertType(multiplier, Float.class))).floatValue();
        }
        if ((source instanceof Long) || (multiplier instanceof Long)) {
            return BigDecimal.valueOf(ObjectTypeUtils.convertType(source, Long.class)).multiply(BigDecimal.valueOf(ObjectTypeUtils.convertType(multiplier, Long.class))).longValue();
        }
        return BigDecimal.valueOf(ObjectTypeUtils.convertType(source, Integer.class)).multiply(BigDecimal.valueOf(ObjectTypeUtils.convertType(multiplier, Integer.class))).intValue();
    }
}
